package com.lianniu.mgr.baseService.post.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.post.service.IAssignAuthorityService;
import com.lianniu.mgr.baseService.post.service.ICheckAuthorityService;

@Controller
@RequestMapping("/rest")
public class CheckAuthorityRestController {
  @Resource
  private ICheckAuthorityService checkAuthorityService;
  @Resource
  private IAssignAuthorityService assignAuthorityService;

  private final static Logger logger = Logger.getLogger(CheckAuthorityRestController.class);

  /**
   * @Description:查看菜单权限
   * @return BaseResult

   * @Date:2016年3月29日下午8:49:36
   * @Version 1.0.0
   */
  @RequestMapping(value = "/menuAuthority",method=RequestMethod.GET)
  public @ResponseBody
  BaseResult menuAuthorityList(HttpServletRequest request) {
    logger.info("CheckAuthorityRestController menuAuthorityList begin.");
    logger.info("postId:"+request.getParameter("postId"));
    
    String postId=request.getParameter("postId");
    BaseResult baseResult1=null;
    BaseResult baseResult2=null;
    try {
      logger.info("getSysMenuByPostId begin.");
      baseResult1 = assignAuthorityService.getSysMenuByPostId(postId);
      logger.info("getSysMenuByPostId end.");
    } catch (Exception e) {
      logger.error("getSysMenuByPostId error:" + e.getMessage());
      logger.info("CheckAuthorityRestController menuAuthorityList end.");
      return BaseResult.exception(e.getMessage());
    }
    try{
      logger.info("getOpMenuByPostId begin.");
      baseResult2=checkAuthorityService.getOpMenuByPostId(postId);
      logger.info("getOpMenuByPostId end.");
    }catch(Exception e){
      logger.info("getOpMenuByPostId error:"+e.getMessage());
      logger.info("CheckAuthorityRestController menuAuthorityList end.");
      return BaseResult.exception(e.getMessage());
    }
    
    Map<String,Object> retMap=new HashMap<String,Object>();
    retMap.put("sysMenu", baseResult1.getData());
    retMap.put("opMenu", baseResult2.getData());
    
    logger.info("CheckAuthorityRestController menuAuthorityList end.");
    baseResult1.setData(retMap);
    return baseResult1;
  }

  /**
   * @Description:查看辖区权限
   * @return BaseResult

   * @Date:2016年3月29日下午8:49:46
   * @Version 1.0.0
   */
  @RequestMapping(value = "/precinctAuthority",method=RequestMethod.GET)
  public @ResponseBody
  BaseResult precinctAuthorityList(HttpServletRequest request) {
    logger.info("CheckAuthorityRestController precinctAuthorityList begin.");
    logger.info("postId:"+request.getParameter("postId"));
    String postId=request.getParameter("postId");
    try {
      logger.info("getPrecinctAuthorityByPostId begin.");
      BaseResult baseResult = checkAuthorityService.getPrecinctAuthorityByPostId(postId);
      logger.info("getPrecinctAuthorityByPostId end.");
      logger.info("CheckAuthorityRestController precinctAuthorityList end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("getPrecinctAuthorityByPostId error:" + e.getMessage());
      logger.info("CheckAuthorityRestController precinctAuthorityList end.");
      return BaseResult.exception(e.getMessage());
    }
  }
}
